/** 
 * @file test-storage.c
 * 
 * Author: Tanshi Li
 * 
 * (C) Copyright 2025 QM Electronics Co., Ltd
 *

 */

#include <common.h>

#include <cli.h>
#include <dm.h>
#include <environment.h>
#include <malloc.h>
#include <misc.h>
#ifdef CONFIG_RKIMG_BOOTLOADER
#include <sysmem.h>
#endif
#include <linux/ctype.h>
#ifdef CONFIG_QM_NVM_VENDOR_PARTITION
#include <asm/arch/nvm_vendor.h>
#endif
#include "test-qm.h"

#ifdef CONFIG_QM_NVM_VENDOR_PARTITION
static int do_test_nvmvendor(cmd_tbl_t *cmdtp, int flag,
			  int argc, char *const argv[])
{
	char *cmd, *s;
	char buf[64];
	int i;
	int ret;

	for (i = 0; i < argc ; i++)
		printf("argv[%d]: %s\n", i, argv[i]);

	/* if only one arguments, goto testall.*/
	if (argc < 2)
		goto testall;
	// for arguments > 2
	cmd = argv[1];

	if (strcmp(cmd, "reset") == 0) {

		putc('\n');
		printf("Reset the vendor storage space to the initial state. \n");
		vendor_test_reset();
		return 0;
	}
	if (strcmp(cmd, "write") == 0) {

		putc('\n');
		printf("write item data test\n");
		sprintf(buf,"test id 10");
		vendor_storage_write(10, buf, 20);
		sprintf(buf,"test id 20");
		vendor_storage_write(20, buf, 20);

		return 0;
	}
	if (strcmp(cmd, "read") == 0) {

		putc('\n');
		printf("read one item data test\n");
		ret = vendor_storage_read(10, buf, 20);
		if (ret < 0) {
			printf("No data for id 10\n");
		}
		else {
			buf[21] = 0;
			printf("id10=%s\n",buf);
		}

		ret = vendor_storage_read(20, buf, 20);
		if (ret < 0) {
			printf("No data for id 20\n");
		}
		else {
			buf[21] = 0;
			printf("id20=%s\n",buf);
		}

		return 0;
	}


testall:

	return vendor_storage_test();
}
#endif


static cmd_tbl_t sub_cmd[] = {

#ifdef CONFIG_QM_NVM_VENDOR_PARTITION
	UNIT_CMD_DEFINE(nvmvendor, 0),
#endif
};

static char sub_cmd_help[] =
#ifdef CONFIG_QM_NVM_VENDOR_PARTITION
"    [.] qmtest nvmvendor                      - test nvmvendor storage read/write\n"
#endif
;

const struct cmd_group cmd_grp_storage = {
	.id	= TEST_ID_STORAGE,
	.help	= sub_cmd_help,
	.cmd	= sub_cmd,
	.cmd_n	= ARRAY_SIZE(sub_cmd),
};
